<template>
  <div class="ExChangeOrder">
    <van-cell-group class="cards" :border="false" inset @click="jump">
      <van-cell class="address" :title="concatAddress" :border="false" is-link center>
        <template #label>
          <span>{{address?.name}}</span>
          <span class="tel">{{address?.phone}}</span>
        </template>
      </van-cell>
    </van-cell-group>
    <van-cell-group class="cards" :border="false" inset>

      <van-cell class="shopurl" :title="shop.shop_name" :icon="`${resourceURL}${shop.imagesurl}`" :border="false" is-link center />
      <van-card class="product" :num="$store.state.stepper" :desc="stock" :title="product.product_name" :thumb="`${resourceURL}${product.imageurl}`" />

    </van-cell-group>
    <!-- <div class="priceBox" v-if="Number(product.express_fee)>0">
      <div class="left">配送服务</div>
      <div class="right">快递 ￥{{product.express_fee}}</div>
    </div> -->
    <van-cell-group class="cards" :border="false" inset>
      <van-cell title="配送服务" value="免邮费" :border="false" center />
    </van-cell-group>

    <van-submit-bar button-text="提交订单" @submit="onSubmit">
      <div class="left">
        <div class="count">共{{$store.state.stepper}}件商品</div>

      </div>
      <div class="integral">-{{($store.state.integral*$store.state.stepper)}}积分</div>
    </van-submit-bar>
  </div>
</template>
<script>
import { Toast } from 'vant'

export default {
  name: '',
  components: {

  },
  mixins: [],
  props: {

  },
  data () {
    return {
      address: {},
      product: {},
      stock: '',
      shop: {}
    }
  },
  computed: {
    // 拼接完整地址
    concatAddress () {
      if (this.address.province_name) {

        return this.address?.province_name + this.address?.city_name + this.address?.area_name + this.address?.detail
      } else {
        return ''
      }
    },
  },
  watch: {

  },
  mounted () {

  },
  methods: {
    // 获取收货地址
    getDefaultaddress () {
      this.$axios.get(this.$api.getdefaultaddress).then(res => { this.address = res.data.data })
    },
    // 获取商品详情
    getProductDetail () {

      this.$axios.get(this.$api.getproductdetail, {
        params: {
          product_id: this.$store.state.product_id
        }
      }).then(res => {
        this.product = res.data.data.product
        this.shop = res.data.data.shop
        res.data.data.stock.forEach(ele => {
          if (ele.p_s_id == this.$store.state.stock_id) {
            this.stock = ele.p_s_name
          }
        });
      })
    },
    onSubmit () {
      this.$axios.post(this.$api.uncommonplace, {
        product_id: this.$store.state.product_id,
        stock_id: this.$store.state.stock_id,
        quantity: this.$store.state.stepper,
        address_id: this.address.address_id
      }).then((res) => {
        if (res.data.code == 1) {
          Toast.success(res.data.msg);
          this.$router.go(-1)
        } else {
          Toast.fail(res.data.msg)
        }
      })
    },
    //跳转至选择地址页
    jump () {
      this.$router.push({
        name: 'chooseAddress',
        query: {
          name: 'ExChangeOrder'
        }
      })
    }

  },
  created () {

    this.getProductDetail()
    if (this.$route.query.item) {
      this.address = JSON.parse(this.$route.query.item)
      console.log(this.address);
    } else {
      this.getDefaultaddress()
    }
  },
};
</script>
<style lang='less' scoped>
.cards {
  margin-bottom: 12px;
  /deep/.van-icon__image {
    width: 100%;
    height: 100%;
  }
  .address {
    /deep/.van-cell__title {
      font-size: 14px;
      font-weight: bold;
    }
    /deep/.van-cell__label {
      font-size: 14px;
      font-weight: 500;
    }
  }
  .shopurl {
    .van-cell__left-icon {
      display: flex;
      align-items: center;
      width: 20px;
      height: 22px;
      margin-right: 8px;
    }
  }
  .product {
    margin-top: 0;
    background-color: #fff;
    /deep/.van-card__title {
      font-size: 14px;
    }
    /deep/ .van-card__price {
      font-size: 14px;
    }
  }
  .price_detail {
    font-weight: bold;
  }
  .coupon {
    .van-cell__left-icon {
      display: flex;
      align-items: center;
      width: 22px;
      height: 22px;
      margin-right: 8px;
    }
    .not_coupon {
      font-size: 12px;
      color: #bbb9b7;
    }
    .is_coupon {
      font-size: 12px;
      color: #fb2b2c;
    }
  }
}
.ExChangeOrder {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  .addressBox {
    width: 343px;
    height: 94px;
    background: #ffffff;
    border: 1px solid #f2f2f2;
    opacity: 1;
    border-radius: 10px;
    box-sizing: border-box;
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 16px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 18px;
    color: #201d1d;
    .left {
      width: 275px;
      margin-right: 10px;
      .address {
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        line-height: 18px;
        color: #201d1d;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        line-clamp: 2;
        -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
      .bottom {
        margin-top: 6px;
        display: flex;
        .name,
        .phone {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          line-height: 18px;
          color: rgba(25, 23, 20, 0.6);
        }
        .phone {
          margin-left: 12px;
        }
      }
    }
  }
  .priceBox {
    margin-top: 12px;
    width: 343px;
    height: 44px;
    background: #ffffff;
    border: 1px solid #f2f2f2;
    opacity: 1;
    border-radius: 10px;
    box-sizing: border-box;
    padding: 13px 16px;
    display: flex;
    justify-content: space-between;
  }
  .count {
    width: 130px;
    text-align: end;
    font-size: 12px;
    color: rgba(34, 32, 32, 0.3);
    height: 32px;
    box-sizing: border-box;
    line-height: 30px;
  }
}
.integral {
  position: absolute;
  top: 25px;
  left: 48%;
  font-size: 12px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  line-height: 18px;
  color: #fb2b2c;
}
.van-submit-bar__bar {
  align-items: flex-start;
  button,
  .van-submit-bar__text,
  .left {
    margin-top: 5px;
    margin-right: 10px;
  }
}
.cards {
  overflow: auto;
  margin-top: 15px;
  width: 345px;
  .address {
    /deep/.van-cell__title {
      font-size: 14px;
      font-weight: bold;
    }
    /deep/.van-cell__label {
      font-size: 14px;
      font-weight: 500;
    }
  }
}
</style>